package com.jzy.stock.kufangguishu.service.impl;

import com.jzy.stock.kufangguishu.mapper.GuiShuMapper;
import com.jzy.stock.kufangguishu.model.*;
import com.jzy.stock.kufangguishu.service.GuiShuService;
import com.jzy.stock.util.AllRecordsVo;
import com.jzy.stock.util.MateDateShengJiEnum;
import com.jzy.stock.util.ResultBuildVo;
import com.jzy.stock.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by PC on 2022/7/11.
 */
@Service
public class GuiShuServiceImpl implements GuiShuService{
   @Autowired
   private GuiShuMapper guiShuMapper;

    //分页
    @Override
    public ResultVo queryPage(KuFangGuiShuSelectVo kuFangGuiShuSelectVo) {
        List<ReservoirAreaVo> ReservoirAreaVos = guiShuMapper.queryData(kuFangGuiShuSelectVo);
        for(ReservoirAreaVo ReservoirAreaVo : ReservoirAreaVos){
            Integer status =  ReservoirAreaVo.getStatus()?1:0;
            ReservoirAreaVo.setStatusName(MateDateShengJiEnum.queryNameById(status));
        }
        int count = guiShuMapper.queryCount(kuFangGuiShuSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(ReservoirAreaVos);//数据
        allRecordsVo.setPageIndex(kuFangGuiShuSelectVo.getPageIndex());
        allRecordsVo.setPageSize(kuFangGuiShuSelectVo.getPageSize());
        allRecordsVo.setTotalNumber(count);// 总数量
        allRecordsVo.setTotalPage(allRecordsVo.countPage(count));// 总页数赋值
        return ResultBuildVo.success(allRecordsVo);
    }
    //法人下拉
    @Override
    public ResultVo querylegalPerson(FaRenVo faRenVo) {
        UserVo userVo = new UserVo();
        userVo.setId(1);
        List<FaRenVo> faRenVoList =  guiShuMapper.queryBylegalPersonName(userVo);
        return ResultBuildVo.success(faRenVoList);
    }

    //启用禁用
    @Override
    public ResultVo updateStatus(ReservoirAreaVo ReservoirAreaVo) {
        ReservoirAreaVo reservoirAreaVoId = guiShuMapper.queryById(ReservoirAreaVo.getId());
        if (reservoirAreaVoId ==null){
            return ResultBuildVo.error("修改的数据不存在");
        }
        UserVo userVo  = new UserVo();
        userVo.setUserName("张三");
        ReservoirAreaVo stockidentityUpdate = new ReservoirAreaVo();
        Date nowDate = new Date();
        stockidentityUpdate.setUpdateTime(nowDate);
        stockidentityUpdate.setUpdateName(userVo.getUserName());
        BeanUtils.copyProperties(ReservoirAreaVo,stockidentityUpdate);
        guiShuMapper.updateStatus(stockidentityUpdate);
        return ResultBuildVo.success();
    }

    //查看按钮根据库房id查库房、库区、库位
    @Override
    public ResultVo see(StockroomVo stockroomVo) {
        //查出来的库房、库区、库位放入一个对象内并返回给前端
        ListVo listVo = new ListVo();
        //根据选中的id查询库房
        StockroomVo stockroomVo1 = guiShuMapper.queryByKuFangId(stockroomVo.getId());
        if (stockroomVo1==null){
            return ResultBuildVo.error("查询的库房不存在");
        }

        listVo.setStockroomList(stockroomVo1);
        //返回的库房code做条件查询库房下的库区
        List<ReservoirAreaVo> reservoirAreaVoList = guiShuMapper.queryByKuFangCode(stockroomVo1.getCode());
        if (reservoirAreaVoList ==null){
            return ResultBuildVo.error("查询的库区不存在");
        }
        listVo.setStockidentityList(reservoirAreaVoList);
        //返回的库区code做条件查询库房下的库位
        for(ReservoirAreaVo ReservoirAreaVo : reservoirAreaVoList){
            List<KuWeiVo>kuWeiVoList = guiShuMapper.queryKuquCode(ReservoirAreaVo.getCode());
            if (kuWeiVoList==null){
                return ResultBuildVo.error("查询的库位不存在");
            }
            listVo.setKuWeiList(kuWeiVoList);
        }

        return ResultBuildVo.success(listVo);
    }

    @Override
    public List<queryAllVo> queryKuFang() {
        ListVo listVo = new ListVo();
        List<queryAllVo>queryKuFangAll =  guiShuMapper.queryKuFangAll();

        for(queryAllVo queryAllVo: queryKuFangAll){

            List<queryAllVo>queryKuQu = guiShuMapper.queryKuQuAll(queryAllVo.getKuFangCode());
            for (queryAllVo queryAllVo1:queryKuQu){
                List<queryAllVo>queryAllVos = guiShuMapper.queryByKuWeiAll(queryAllVo1.getKuQuCode());
            }
        }
        return null;
    }


}
